#include "M79Racruvido.h"
#include <GL/glu.h>
#include "gllib/Objects.h"
#include "src/mathlib/Matrix4.h"
#include <cmath>

void M79Racruvido::draw_cano()
{
    double cano_raio = 0.2;
    double cano_comprimento = 1.02;
    double cano_precisao = 10.0;
    double mira_lado = 0.1;

    // Trava do cano:
    glColor3f(0.10,0.10,0.10);
    glPushMatrix();
        translationMatrix(Vector3(0.0,0.2,0.5)).applyGL();
        scaleMatrix(1.0,0.4,10.0).applyGL();
        gllibCube(mira_lado);
    glPopMatrix();
    // Cano:
    glColor3f(0.15,0.15,0.15);
    glPushMatrix();
        translationMatrix(Vector3(0.0,0.0,cano_comprimento)).applyGL();
        rotationMatrix(rad(90),Vector3(1.0,0.0,0.0)).applyGL();
        scaleMatrix(1.0,2.0,1.0).applyGL();
        gllibCylinder(cano_raio,cano_comprimento,cano_precisao);
    glPopMatrix();
    // "Buraco" do cano:
    glColor3f(0.0, 0.0, 0.0);
    glPushMatrix();
        translationMatrix(Vector3(0.0,0.0,2*cano_comprimento+0.001)).applyGL();
        rotationMatrix(rad(90),Vector3(1.0,0.0,0.0)).applyGL();
        gllibCylinder((0.8)*cano_raio,0.001,cano_precisao);
    glPopMatrix();
    // Mira na ponta do cano:
    glColor3f(0.0,0.0,0.0);
    glPushMatrix();
        translationMatrix(Vector3(0.0,cano_raio+(mira_lado/2),(1.9)*cano_comprimento)).applyGL();
        scaleMatrix(1.2,2.0,0.2).applyGL();
        gllibCube(mira_lado);
    glPopMatrix();
}

void M79Racruvido::draw_suporte()
{
    double suporte_lado = 0.34;
    double suporte_comprimento = 4.0;

    glPushMatrix();
        translationMatrix(Vector3(0.0,-0.2,(suporte_lado*suporte_comprimento)/2)).applyGL();
        scaleMatrix(1.0,0.5,suporte_comprimento).applyGL();
        gllibCube(suporte_lado);
    glPopMatrix();
}

void M79Racruvido::draw_corpo()
{
    double corpo_raio = 0.21;
    double corpo_altura = 1.0;
    double corpo_precisao = 10.0;

    glPushMatrix();
        translationMatrix(Vector3(0.0,-0.20,-corpo_altura/2)).applyGL();
        rotationMatrix(rad(-90),Vector3(1.0,0.0,0.0)).applyGL();
        shearMatrix(76).applyGL();
        scaleMatrix(1.0,1.0,1.25).applyGL();
        gllibCone(corpo_raio,corpo_altura,corpo_precisao);
    glPopMatrix();
}

void M79Racruvido::draw_cabo()
{
    double cabo_raio = 0.22;
    double cabo_altura = 1.0;
    double cabo_precisao = 10.0;

    glPushMatrix();
        translationMatrix(Vector3(0.0,-0.19,-0.85)).applyGL();
        rotationMatrix(rad(90),Vector3(1.0,0.0,0.0)).applyGL();
        shearMatrix(-75).applyGL();
        scaleMatrix(0.8,1.0,1.5).applyGL();
        gllibCone(cabo_raio,cabo_altura,cabo_precisao);
    glPopMatrix();
}

void M79Racruvido::draw_gatilho()
{
    double gatilho_raio_interno = 0.04;
    double gatilho_raio_externo = 0.2;
    double gatilho_rebaixamento = -0.3;
    double gatilho_click_raio = 0.04;

    // Proteção do gatilho:
    glColor3f(0.2, 0.2, 0.2);
    glPushMatrix();
        translationMatrix(Vector3(0.0,gatilho_rebaixamento,0.15)).applyGL();
        rotationMatrix(rad(30),Vector3(1.0,0.0,0.0)).applyGL();
        rotationMatrix(rad(90),Vector3(0.0,0.0,1.0)).applyGL();
        gllibTorus(gatilho_raio_interno,gatilho_raio_externo,2.0,3.0);
    glPopMatrix();
    // Gatilho em si:
    glColor3f(0.9,0.3,0.3);
    glPushMatrix();
        translationMatrix(Vector3(0.0,-0.33,0.15)).applyGL();
        shearMatrix(70).applyGL();
        scaleMatrix(1.0,3.0,1.0).applyGL();
        gllibCube(gatilho_click_raio);
    glPopMatrix();
}

void M79Racruvido::draw_mira()
{
    double mira_raio_interno = 0.04;
    double mira_raio_externo = 0.15;
    double mira_suporte_raio = 0.2;

    glPushMatrix();
        translationMatrix(Vector3(0.0,0.0,1.2)).applyGL();
        rotationMatrix(rad(90),Vector3(1.0,0.0,0.0)).applyGL();
        // Mira:
        glPushMatrix();
            translationMatrix(Vector3(0.0,0.0,-0.6)).applyGL();
            scaleMatrix(1.0,0.5,3.0).applyGL();
            rotationMatrix(rad(45),Vector3(0.0,1.0,0.0)).applyGL();
            gllibTorus(mira_raio_interno,mira_raio_externo,2.0,2.0);
        glPopMatrix();
        // Suporte:
        glPushMatrix();
            scaleMatrix(2.5,0.2,2.5).applyGL();
            gllibCube(mira_suporte_raio);
        glPopMatrix();
    glPopMatrix();
}

void M79Racruvido::draw()
{
//    gllibEixoY();

    draw_cano();

    glColor3f(0.5, 0.50, 0.39);
    draw_suporte();

    draw_corpo();

    draw_cabo();

    draw_gatilho();

    glColor3f(0.1,0.1,0.1);
    draw_mira();
}
